package me.lc;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.admogo.AdMogoLayout;
import com.admogo.AdMogoListener;
import com.admogo.AdMogoManager;
import com.mobclick.android.MobclickAgent;

public class FunPeaceActivity extends Activity implements OnGestureListener,
		AdMogoListener {

	private boolean isLoaded = false;
	private ProgressDialog progressDialog;
	private int delaySeconds = 0;
	private int countDown = 0;
	private View page1;
	private View page2;
	private View page3;
	private ViewFlipper vf;
	private GestureDetector gd;
	private int pageIndex = 0;
	private PageControlView pageControler;
	private SharedPreferences settings;
	private AdMogoLayout adLayout;
	String isMoneyOn;

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		AdMogoManager.clear();
		super.onDestroy();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		MobclickAgent.onError(this);
		MobclickAgent.updateOnlineConfig(this);
		SoundManager.instance.setContext(this);
		settings = this.getPreferences(Activity.MODE_PRIVATE);
		if (!isLoaded) {
			isLoaded = true;
			setUI();
			Timer countDownTimer = new Timer();
			countDownTimer.schedule(new CountDownTask(), 1000, 1000);
		}
	}

	private void initNewPage() {
		// isMoneyOn = MobclickAgent.getConfigParams(this, "isMoneyOnAnzhuo");
		isMoneyOn = MobclickAgent.getConfigParams(this, "isMoneyOnAnzhuo225");
		System.out.println("webPara:" + isMoneyOn);
		if (isMoneyOn.equals("")) {
			isMoneyOn = "false";
			// isMoneyOn = "true";
		}
		String status = settings.getString("lockStatus", "");
		boolean isfirst = settings.getBoolean("isfirst", true);
		if (isfirst) {
			dialog("新音效来了！", "放屁精灵2.0版本增加10个新音效，感谢您的支持！");
			Editor editor = settings.edit();
			editor.putBoolean("isfirst", false);
			editor.commit();
		}
		if (status.length() > 0) {
			((TextView) page3.findViewById(R.id.textViewLock))
					.setVisibility(View.GONE);
			((ImageView) page3.findViewById(R.id.imageViewLock))
					.setVisibility(View.GONE);
			((TableLayout) page3.findViewById(R.id.buttonTablePage3))
					.setVisibility(View.VISIBLE);
		} else if (isMoneyOn.equalsIgnoreCase("false")) {
			((TextView) page3.findViewById(R.id.textViewLock))
					.setVisibility(View.GONE);
			((ImageView) page3.findViewById(R.id.imageViewLock))
					.setVisibility(View.GONE);
			((TableLayout) page3.findViewById(R.id.buttonTablePage3))
					.setVisibility(View.VISIBLE);
		}
	}

	public void dialog(String title, final String message) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				countDown--;
				if (countDown >= 0) {
					if (progressDialog != null) {
						progressDialog.setProgress(countDown);
						progressDialog.setMessage("剩余" + countDown + "秒");
					}
				}
				break;
			}
		}
	};

	class CountDownTask extends TimerTask {
		@Override
		public void run() {
			Message msg = new Message();
			msg.what = 1;
			handler.sendMessage(msg);
		}
	}

	class PlaySountTask extends TimerTask {
		private int soundId = 0;

		public PlaySountTask(int soundId) {
			this.soundId = soundId;
		}

		@Override
		public void run() {
			if (progressDialog != null) {
				progressDialog.cancel();
			}
			SoundManager.instance.playSound(soundId);
		}
	}

	private void setUI() {
		OnClickListener lis = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int soundIndex = 0;
				switch (v.getId()) {
				case R.id.button1:
					soundIndex = 0;
					break;
				case R.id.button2:
					soundIndex = 1;
					break;
				case R.id.button3:
					soundIndex = 2;
					break;
				case R.id.button4:
					soundIndex = 3;
					break;
				case R.id.button5:
					soundIndex = 4;
					break;
				case R.id.button6:
					soundIndex = 5;
					break;
				case R.id.button7:
					soundIndex = 6;
					break;
				case R.id.button8:
					soundIndex = 7;
					break;
				case R.id.button9:
					soundIndex = 8;
					break;
				case R.id.button10:
					soundIndex = 9;
					break;
				case R.id.button11:
					soundIndex = 10;
					break;
				case R.id.button12:
					soundIndex = 11;
					break;
				case R.id.button13:
					soundIndex = 12;
					break;
				case R.id.button14:
					soundIndex = 13;
					break;
				case R.id.button15:
					soundIndex = 14;
					break;
				case R.id.button16:
					soundIndex = 15;
					break;
				case R.id.button17:
					soundIndex = 16;
					break;
				case R.id.button18:
					soundIndex = 17;
					break;
				case R.id.button19:
					soundIndex = 18;
					break;
				case R.id.button20:
					soundIndex = 19;
					break;
				case R.id.button21:
					soundIndex = 20;
					break;
				case R.id.button22:
					soundIndex = 21;
					break;
				case R.id.button23:
					soundIndex = 22;
					break;
				case R.id.button24:
					soundIndex = 23;
					break;
				case R.id.button25:
					soundIndex = 24;
					break;
				case R.id.button26:
					soundIndex = 25;
					break;
				case R.id.button27:
					soundIndex = 26;
					break;
				case R.id.button28:
					soundIndex = 27;
					break;
				case R.id.button29:
					soundIndex = 28;
					break;
				case R.id.button30:
					soundIndex = 29;
					break;
				default:
					soundIndex = -1;
					break;
				}
				Button currentBtn = (Button) v;
				String piName = currentBtn.getText().toString();
				if (soundIndex >= 0) {
					Timer timer = new Timer();
					timer.schedule(new PlaySountTask(soundIndex),
							1000 * delaySeconds);
					if (delaySeconds > 0) {
						progressDialog = new ProgressDialog(
								FunPeaceActivity.this);
						progressDialog
								.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
						progressDialog.setTitle("放屁倒计时（" + piName + "）");
						progressDialog.setIcon(R.drawable.icon);
						progressDialog.setMessage("剩余" + delaySeconds + "秒");
						progressDialog.setCancelable(false);
						progressDialog.setMax(delaySeconds);
						progressDialog.setProgress(delaySeconds);
						progressDialog.show();
						countDown = delaySeconds;
					} else {
						progressDialog = null;
					}
				}
			}
		};
		adLayout = (AdMogoLayout) findViewById(R.id.admogo_layout);
		adLayout.setAdMogoListener(this);
		FriendlyScrollView sv = (FriendlyScrollView) this
				.findViewById(R.id.friendlyScrollView);
		page1 = this.getLayoutInflater().inflate(R.layout.page1, null);
		page2 = this.getLayoutInflater().inflate(R.layout.page2, null);
		page3 = this.getLayoutInflater().inflate(R.layout.page3, null);
		page1.setLongClickable(true);
		page2.setLongClickable(true);
		page3.setLongClickable(true);
		gd = new GestureDetector(this);
		sv.setGestureDetector(gd);
		pageControler = (PageControlView) this.findViewById(R.id.pageControl);
		vf = (ViewFlipper) this.findViewById(R.id.myViewflipper);
		vf.addView(page1, 0);
		vf.addView(page2, 1);
		vf.addView(page3, 2);
		Button btn1 = (Button) page1.findViewById(R.id.button1);
		Button btn2 = (Button) page1.findViewById(R.id.button2);
		Button btn3 = (Button) page2.findViewById(R.id.button3);
		Button btn4 = (Button) page2.findViewById(R.id.button4);
		Button btn5 = (Button) page1.findViewById(R.id.button5);
		Button btn6 = (Button) page1.findViewById(R.id.button6);
		Button btn7 = (Button) page2.findViewById(R.id.button7);
		Button btn8 = (Button) page2.findViewById(R.id.button8);
		Button btn9 = (Button) page1.findViewById(R.id.button9);
		Button btn10 = (Button) page1.findViewById(R.id.button10);
		Button btn11 = (Button) page2.findViewById(R.id.button11);
		Button btn12 = (Button) page2.findViewById(R.id.button12);
		Button btn13 = (Button) page1.findViewById(R.id.button13);
		Button btn14 = (Button) page1.findViewById(R.id.button14);
		Button btn15 = (Button) page2.findViewById(R.id.button15);
		Button btn16 = (Button) page2.findViewById(R.id.button16);
		Button btn17 = (Button) page1.findViewById(R.id.button17);
		Button btn18 = (Button) page1.findViewById(R.id.button18);
		Button btn19 = (Button) page2.findViewById(R.id.button19);
		Button btn20 = (Button) page2.findViewById(R.id.button20);

		Button btn21 = (Button) page3.findViewById(R.id.button21);
		Button btn22 = (Button) page3.findViewById(R.id.button22);
		Button btn23 = (Button) page3.findViewById(R.id.button23);
		Button btn24 = (Button) page3.findViewById(R.id.button24);
		Button btn25 = (Button) page3.findViewById(R.id.button25);
		Button btn26 = (Button) page3.findViewById(R.id.button26);
		Button btn27 = (Button) page3.findViewById(R.id.button27);
		Button btn28 = (Button) page3.findViewById(R.id.button28);
		Button btn29 = (Button) page3.findViewById(R.id.button29);
		Button btn30 = (Button) page3.findViewById(R.id.button30);

		btn1.setOnClickListener(lis);
		btn2.setOnClickListener(lis);
		btn3.setOnClickListener(lis);
		btn4.setOnClickListener(lis);
		btn5.setOnClickListener(lis);
		btn6.setOnClickListener(lis);
		btn7.setOnClickListener(lis);
		btn8.setOnClickListener(lis);
		btn9.setOnClickListener(lis);
		btn10.setOnClickListener(lis);
		btn11.setOnClickListener(lis);
		btn12.setOnClickListener(lis);
		btn13.setOnClickListener(lis);
		btn14.setOnClickListener(lis);
		btn15.setOnClickListener(lis);
		btn16.setOnClickListener(lis);
		btn17.setOnClickListener(lis);
		btn18.setOnClickListener(lis);
		btn19.setOnClickListener(lis);
		btn20.setOnClickListener(lis);
		btn21.setOnClickListener(lis);
		btn22.setOnClickListener(lis);
		btn23.setOnClickListener(lis);
		btn24.setOnClickListener(lis);
		btn25.setOnClickListener(lis);
		btn26.setOnClickListener(lis);
		btn27.setOnClickListener(lis);
		btn28.setOnClickListener(lis);
		btn29.setOnClickListener(lis);
		btn30.setOnClickListener(lis);

		SeekBar secondBar = (SeekBar) findViewById(R.id.seekBarSecond);
		secondBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				TextView txtNote = (TextView) findViewById(R.id.txtTime);
				txtNote.setText("放屁延时：" + progress + "秒");
				delaySeconds = progress;
			}
		});

		SeekBar volumeBar = (SeekBar) findViewById(R.id.seekBarVolume);
		volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				SoundManager.instance.setVolume((float) (progress / 100.0));
			}
		});
		volumeBar.setProgress((int) (SoundManager.instance.getVolume() * 100));
		pageControler.setCount(3);
		pageControler.generatePageControl(0);
		pageControler.postInvalidate();
		initNewPage();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gd.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if ((e1.getX() - e2.getX()) > 100) {
			this.vf.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.animation_right_in));
			this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.animation_left_out));
			this.vf.showNext();
			pageIndex++;
			pageIndex = pageIndex % 3;
			pageControler.generatePageControl(pageIndex);
			return true;
		} else if ((e1.getX() - e2.getX()) < -100) {
			this.vf.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.animation_left_in));
			this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.animation_right_out));
			this.vf.showPrevious();

			if (pageIndex == 0) {
				pageIndex = 2;
			} else {
				pageIndex--;
			}
			pageControler.generatePageControl(pageIndex);
			return true;
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onClickAd() {
		// unlock
		if (!isMoneyOn.equalsIgnoreCase("false")) {
			Editor editor = settings.edit();
			editor.putString("lockStatus", "unlock");
			editor.commit();
			initNewPage();
		}
	}

	@Override
	public void onFailedReceiveAd() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReceiveAd() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCloseMogoDialog() {

	}

	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// return gd.onTouchEvent(event);
	// }
}